package com.nbcio.modules.im.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nbcio.modules.im.domain.ImChatGroup;
import com.nbcio.modules.im.domain.ImChatGroupUser;
import com.nbcio.modules.im.mapper.ImChatGroupMapper;
import com.nbcio.modules.im.service.IImChatGroupService;

import com.nbcio.modules.im.service.IImChatGroupUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 群 服务实现类
 * </p>
 *
 * @author nbacheng
 * @since 2018-10-28
 */
@Service
@Qualifier("imChatGroupServiceImpl")
public class ImChatGroupServiceImpl extends ServiceImpl<ImChatGroupMapper, ImChatGroup> implements IImChatGroupService {


    @Autowired
    private IImChatGroupUserService imChatGroupUserService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addGroupWithUsernames(ImChatGroup group, String usernames){
        //1.插入 im_chat_group 表
        boolean first = this.save(group);
        //2.插入 im_chat_group_user 表
        if(first && StringUtils.isNotBlank(usernames)){
            List<ImChatGroupUser> list = new ArrayList<>();
            String[] uArr = usernames.split(",");
            for(String uId : uArr){
                ImChatGroupUser u = new ImChatGroupUser();
                u.setChatGroupId(group.getId());
                u.setUserId(uId);
                u.setCreateDate(new Date());
                list.add(u);
            }
            imChatGroupUserService.saveBatch(list);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editGroupWithUsernames(ImChatGroup group, String usernames){
        boolean first = this.updateById(group);
        if(first){
            QueryWrapper<ImChatGroupUser> delQuery = new QueryWrapper<>();
            delQuery.eq("chat_group_id",group.getId());
            imChatGroupUserService.remove(delQuery);
            List<ImChatGroupUser> list = new ArrayList<>();
            String[] uArr = usernames.split(",");
            for(String uId : uArr){
                ImChatGroupUser u = new ImChatGroupUser();
                u.setChatGroupId(group.getId());
                u.setUserId(uId);
                u.setCreateDate(new Date());
                list.add(u);
            }
            imChatGroupUserService.saveBatch(list);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delGroupWithUsernames(ImChatGroup group, String usernames){
        QueryWrapper<ImChatGroupUser> delQuery = new QueryWrapper<>();
        delQuery.eq("chat_group_id",group.getId());
        boolean first = imChatGroupUserService.remove(delQuery);
        if(first){
            this.removeById(group);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelGroup(String ids){
        QueryWrapper<ImChatGroupUser> delQuery = new QueryWrapper<>();
        delQuery.in("chat_group_id", Arrays.asList(ids.split(",")));
        boolean first = imChatGroupUserService.remove(delQuery);
        if(first){
            this.removeBatchByIds(Arrays.asList(ids.split(",")));
        }
    }
}
